package com.labelimage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.labelimage.bean.entity.ImageEntity;
import com.labelimage.bean.entity.LabelImageEntity;
import com.labelimage.bean.param.LabelImageParam;
import com.labelimage.bean.vo.LabelImageVo;
import com.labelimage.dao.LabelImageDao;
import com.labelimage.service.ImageService;
import com.labelimage.service.LabelImageService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


@Service("labelImageService")
public class LabelImageServiceImpl extends ServiceImpl<LabelImageDao, LabelImageEntity> implements LabelImageService {

    @Resource
    private ImageService imageService;

    @Override
    public List<LabelImageVo> getRandom(Long templateId, Integer num, List<Long> unLabelImageIds) {
        LambdaQueryWrapper<LabelImageEntity> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper
                .eq(LabelImageEntity::getTemplateId, templateId)
                .isNull(LabelImageEntity::getLabelId);
        if (!unLabelImageIds.isEmpty())
        {
            lambdaQueryWrapper.notIn(LabelImageEntity::getId,unLabelImageIds);
        }
        lambdaQueryWrapper.last("limit " + (num * 10));
        List<LabelImageEntity> labelImageEntityList = this.getBaseMapper().selectList(lambdaQueryWrapper);
        List<LabelImageEntity> select = new ArrayList<>();
        if (labelImageEntityList.size() <= num) {
            select = labelImageEntityList;
        } else {
            for (int i = 0; i < num; i++) {
                int index = (int) (Math.random() * labelImageEntityList.size());
                select.add(labelImageEntityList.get(index));
                labelImageEntityList.remove(index);
            }
        }

        return select.stream().map(labelImageEntity -> {
            LabelImageVo labelImageVo = LabelImageVo.builder().build();

            ImageEntity imageEntity = imageService.getById(labelImageEntity.getImageId());
            labelImageVo.setLabelImageId(labelImageEntity.getId());
            labelImageVo.setPath(imageEntity.getPath());
            labelImageVo.setImageName(imageEntity.getImageName());

            return labelImageVo;
        }).collect(Collectors.toList());

    }

    @Override
    public void labelImage(LabelImageParam labelImageParam) {
        for (Long id : labelImageParam.getLabelImageIds()) {
            Optional<LabelImageEntity> labelImageEntityOptional = this.lambdaQuery().eq(LabelImageEntity::getId, id).oneOpt();
            if (labelImageEntityOptional.isPresent())
            {
                labelImageEntityOptional.get().setLabelId(labelImageParam.getLabelId());
                this.updateById(labelImageEntityOptional.get());
            }
        }

    }


}